package cn.com.chengmandian.bike.controller;


import cn.com.chengmandian.agent.utils.AuthUtil;
import cn.com.chengmandian.bike.dto.MemberCouponDto;
import cn.com.chengmandian.bike.model.EbRideOrder;
import cn.com.chengmandian.bike.model.vo.MemberCouponVo;
import cn.com.chengmandian.bike.service.IEbMemberCouponService;
import cn.com.chengmandian.bike.service.IEbRideOrderService;
import cn.com.chengmandian.core.db.pojo.PageData;
import cn.com.chengmandian.core.web.pojo.AgentInfo;
import cn.com.chengmandian.core.web.pojo.AjaxResult;
import cn.com.chengmandian.core.web.utils.LocalMapUtil;
import cn.com.chengmandian.service.bike.constants.CouponInfoTypeEnum;
import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 电单车运营-用户优惠券表 前端控制器
 * </p>
 *
 * @author AutoGenerator
 * @since 2021-12-15
 */
@Api(tags = "用户优惠券相关接口")
@RestController
@RequestMapping("/member/coupon")
public class EbMemberCouponController {
    @Autowired
    private IEbMemberCouponService memberCouponService;

    @Autowired
    private IEbRideOrderService rideOrderService;

    @ApiOperation("用户优惠券列表")
    @GetMapping("/pages")
    public AjaxResult<PageData<MemberCouponVo>> pages(MemberCouponDto memberCouponDto) {
        AgentInfo agentInfo = LocalMapUtil.getAgentInfo();
        Assert.notNull(agentInfo.getAreaId(), "缺少运营区ID");
        memberCouponDto.setAreaId(agentInfo.getAreaId());
        return AjaxResult.success(PageData.build(memberCouponService.pages(memberCouponDto, AuthUtil.getAgentUser().getId())));
    }

    @ApiOperation("用户可用优惠券列表")
    @GetMapping("/available/list")
    public AjaxResult<List<MemberCouponVo>> availableList() {
        AgentInfo agentInfo = LocalMapUtil.getAgentInfo();
        Assert.notNull(agentInfo.getAreaId(), "缺少运营区ID");
        return AjaxResult.success(memberCouponService.availableList(agentInfo.getAreaId(), AuthUtil.getAgentUser().getId(), null, DateUtil.now()));
    }

    @ApiOperation("订单可用优惠券列表")
    @GetMapping("/order/list/{orderNo}")
    public AjaxResult<List<MemberCouponVo>> orderList(@PathVariable("orderNo") String orderNo) {
        AgentInfo agentInfo = LocalMapUtil.getAgentInfo();
        Assert.notNull(agentInfo.getAreaId(), "缺少运营区ID");
        EbRideOrder rideOrder = rideOrderService.getByNo(orderNo, AuthUtil.getAgentUser().getId());
        Assert.notNull(rideOrder, "订单不存在");
        return AjaxResult.success(memberCouponService.availableList(agentInfo.getAreaId(), AuthUtil.getAgentUser().getId(), CouponInfoTypeEnum.RIDE.getCode(), DateUtil.format(rideOrder.getOpenTime(), "yyyy-MM-dd HH:mm:ss"), rideOrder.getCouponId()));
    }
}
